//
// Created by v on 19-6-11.
//

#ifndef JKLIBS_CMCONFIGCPP_H
#define JKLIBS_CMCONFIGCPP_H

#include "jsoncpp/json/json.h"

class CMConfigCpp
{
  public:
    explicit CMConfigCpp(const char *filename);
    virtual ~CMConfigCpp();

    int toString(std::string &result);

    int writeFile();

    // This will replace original data
    int parse(const std::string &str);

    int getBool(const char *key, bool &result);
    int getString(const char *key, std::string &result);
    int getInt(const char *key, int &result);
    int getObject(const char *key, Json::Value &result);

    // Give copy
    int getRoot(Json::Value &root);

  protected:
    int parseFromFile();

  private:
    std::string m_filename;
    Json::Value m_root;
    // If valid parse from file
    bool m_valid;

    Json::CharReaderBuilder m_builder;
    Json::CharReader *      m_reader;
};

#endif // JKLIBS_CMCONFIGCPP_H
